import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router';
// import {useTopRouteStore} from '@/store/topRoute.js';
// import { whiteList } from '@/utils/util';


/**
 * 引入树结构路由
 * */ 
// 订单路由
// import orderRouter from "./module/orderRouter.js";
// // 经营数据分析路由
// import {businessDataAnaylsisRouter} from "./module/businessDataAnaylsis/index.js";
// // 工具路由
// import toolsRouter from "./module/toolsRouter.js";
// // 工单路由
// import workOrderRouter from "./module/workOrderRouter.js";
// // 商机及线索路由
// import opportunityAndClueRouter from "./module/opportunityAndClue/index.js";
// // 产品配置中心路由
// import productConfigurationRouter from "./module/productConfiguration/index.js";
// // 组织架构路由
// import organizationalStructureRouter from "./module/organizationalStructure/index.js";
// /**
//  * 引入扁平化路由
//  * */ 
// // 经营数据分析路由
// import {businessDataAnaylsisFlattenRouter} from './module/businessDataAnaylsis/index.js';
// // 供应商路由
// import {supplierFlattenRouter} from './module/supplierFlattenRouter.js';

// import {usePermissionsStore} from '@/store/permissions.js';

/**
 * 切记：要缓存的页面，需要是一级或二级路由，否则缓存不生效
 * 
 */

const routes = [
  {
    path: '/login',
    component: () => import('@/pages/login/index.vue'),
    meta: {
      title: '登录',
    }
  },
//   {
//     path: '/404',
//     component: () => import('@/views/noFound/index.vue'),
//     name: '404',
//   },
]
// 除去缓存页面模块外的路由
// export const removeCachedRouter = [
//   ...workOrderRouter,
//   ...opportunityAndClueRouter,
//   ...productConfigurationRouter,
//   ...organizationalStructureRouter,
//   ...orderRouter,
//   ...toolsRouter,
//   {
//     path: '/openQun',
//     name: 'openQun',
//     component: () => import('@/views/test/openQun.vue'),
//     meta: {
//       title: '测试打开群聊',
//       icon: 'iconfont icon-caidan1'
//     },
//   }
// ]
// 需要缓存的路由
// export const keepAliveListRouter = [
//   ...businessDataAnaylsisFlattenRouter,
//   ...supplierFlattenRouter,
// ]
// 左侧菜单需要的路由
// export const menuRouters = {
//   path: '/',
//   component: () => import('@/Layout/index.vue'),
//   name: 'Layout',
//   meta: {
//   },
//   children: [
//     ...businessDataAnaylsisRouter,
//     ...removeCachedRouter,
//   ]
// }

export const routers = {
//   path: '/',
//   component: () => import('@/Layout/index.vue'),
//   name: 'Layout',
//   children: []
}
// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes: [routers, ...routes],
})

// const dynamicRoutesAdded = { value: false };
// router.beforeEach((to, from, next) => {
//   const user = localStorage.getItem('user');
//   const topRouteStore = useTopRouteStore();
//   if (user) {
//     if (!dynamicRoutesAdded.value) {
//       try {
//         const permissionsStore = usePermissionsStore();
//         permissionsStore.dynamicRoutes();
//         dynamicRoutesAdded.value = true;
//         // 确保路由添加完成后，重新加载目标路由
//         next({ ...to, replace: true });
//         return
//       } catch (error) {
//         console.error('获取路由权限失败:', error);
//         topRouteStore.routeList = [];
//         next({ path: '/login' });
//       }
//     }
//     const userInfo = JSON.parse(user);
//     if((!userInfo.accessToken || !userInfo.refreshToken) && to.path !== '/login') {
//       topRouteStore.routeList = [];
//       return next({ path: '/login' });
//     }
//   } else if(!whiteList.includes(to.path) && to.path !== '/login') {
//     topRouteStore.routeList = [];
//     return next({ path: '/login' });
//   }
//   if(to.path === '/login') {
//     topRouteStore.routeList = [];
//     localStorage.removeItem('user');
//   }
//   next();
// })
export default router;
